import scrapy
from my_zdmm.items import MyZdmmItem
import time

# 藏地密码
class ZdmmSpider(scrapy.Spider):
    name = 'zdmm'
    allowed_domains = ['www.dmbj.cc']
    start_urls = ['http://www.dmbj.cc/zangdimima']

    def parse(self, response):

        li_list = response.xpath("//div[@class='xsbox clearfix']//a")

        for li in li_list:
            title = li.xpath('.//text()').extract_first()
            href = li.xpath('.//@href').extract_first()
            print(href)
            time.sleep(10)
            # 访问详情页
            yield scrapy.Request(url=href, callback=self.parse_second, meta={"title": title})

    def parse_second(self, response):
        # 注意 如果拿不到数据的情况下，一定检查你的xpath的语法是否正确
        p_list = response.xpath('//div[@class="m-post"]/p')
        title = response.meta['title']
        content = ''
        for p in p_list:
            content += p.xpath('./text()').extract_first() + '\n'

        content = MyZdmmItem(content=content, title=title)
        yield content
